package com.hankol.message.respository

import androidx.lifecycle.MutableLiveData
import com.baselibrary.base.BasicListBean
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.SpUtils
import com.hankol.message.api.MessageApi
import com.hankol.message.bean.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class MessageRequestManager {
    companion object {
        @Volatile
        private var instance: MessageRequestManager? = null

        fun getInstance(): MessageRequestManager {
            return instance ?: synchronized(this) {
                instance ?: MessageRequestManager().also { instance = it }
            }
        }
    }

    /**
     *点赞和收藏 列表
     */
    fun reqLikeAndCollectList(page: Int, listData: MutableLiveData<BasicListBean<LikeAndCollectBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(MessageApi::class.java).reqLikeAndCollectList(it, page, 10)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<LikeAndCollectBean>>(this) {
                        override fun onSuccess(t: BasicListBean<LikeAndCollectBean>, json: String) {
                            listData.value = t
                        }

                        override fun onFailed(message: String?) {
                            listData.value = null
                            super.onFailed(message)
                        }

                        override fun onError(e: Throwable) {
                            listData.value = null
                            super.onError(e)
                        }
                    })
        }
    }

    /**
     *评论我的 列表
     */
    fun reqCommentMeList(page: Int, listData: MutableLiveData<BasicListBean<CommentMeBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(MessageApi::class.java).reqCommentMeList(it, page, 10)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<CommentMeBean>>(this) {
                        override fun onSuccess(t: BasicListBean<CommentMeBean>, json: String) {
                            listData.value = t
                        }

                        override fun onFailed(message: String?) {
                            listData.value = null
                            super.onFailed(message)
                        }

                        override fun onError(e: Throwable) {
                            listData.value = null
                            super.onError(e)
                        }
                    })
        }
    }

    /**
     *系统消息 列表
     */
    fun reqSystemMsgList(msgPushTypeId:Int,cursor: String, listData: MutableLiveData<BasicListBean<BroadcastMsgBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(MessageApi::class.java).reqSystemMsgList(it,msgPushTypeId, cursor, 10)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<BroadcastMsgBean>>(this) {
                        override fun onSuccess(t: BasicListBean<BroadcastMsgBean>, json: String) {
                            listData.value = t
                        }

                        override fun onFailed(message: String?) {
                            listData.value = null
                            super.onFailed(message)
                        }

                        override fun onError(e: Throwable) {
                            listData.value = null
                            super.onError(e)
                        }
                    })
        }
    }

    //关注人数，粉丝人数
    fun reqAttentiveNum(attentiveNumLD: MutableLiveData<AttentiveNum>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(MessageApi::class.java).reqAttentiveNum(it).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<AttentiveNum>(this) {
                        override fun onSuccess(result: AttentiveNum, json: String?) {
                            attentiveNumLD.value = result
                        }

                        override fun onFailed(message: String?) {
                            super.onFailed(message)
                            attentiveNumLD.value = null
                        }

                    })
        }
    }

    //广播-设置消息为已读
    fun reqBroadcastRead(attentiveNumLD: MutableLiveData<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(MessageApi::class.java).reqBroadcastRead(it).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Boolean>(this) {
                        override fun onSuccess(result: Boolean, json: String?) {
                            attentiveNumLD.value = result
                        }

                        override fun onFailed(message: String?) {
                            super.onFailed(message)
                            attentiveNumLD.value = null
                        }
                    })
        }
    }

    //评论我的列表、赞和收藏列表、新增粉丝列表，3个接口的未读数统计
    fun reqUnReadCnt(attentiveNumLD: MutableLiveData<UnReadCntBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(MessageApi::class.java).reqUnReadCnt(it).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<UnReadCntBean>(this) {
                        override fun onSuccess(result: UnReadCntBean, json: String?) {
                            attentiveNumLD.value = result
                        }

                        override fun onFailed(message: String?) {
                            super.onFailed(message)
                            attentiveNumLD.value = null
                        }
                    })
        }
    }

    //系统消息类型列表
    fun reqGetSysMsgList(mld: MutableLiveData<List<GetSysMsgListBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(MessageApi::class.java).reqGetSysMsgList(it).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<List<GetSysMsgListBean>>(this) {
                        override fun onSuccess(result: List<GetSysMsgListBean>, json: String?) {
                            mld.value = result
                        }

                        override fun onFailed(message: String?) {
                            super.onFailed(message)
                            mld.value = null
                        }
                    })
        }
    }

}